'use strict';

const Controller = require('egg').Controller;

class UserController extends Controller {
  async login() {
    const { ctx, app } =this;
    // post请求传来的参数
    const { id } = ctx.request.body;
    // 判断数据库里面是否存在该用户
    const user = await ctx.service.user.login(id);
    
    if(user){
        console.log(user[0].id)
        // 用户存在,生成token
        const token = app.jwt.sign({
            userID: user[0].id,
            username:user[0].name,
        }, app.config.jwt.secret);
        
        ctx.body = {
        code: 200,
        message:'登录成功',
        data: user,
        token
        }
    } 
  }

  async Info(){
    const { ctx, app } =this;
    // post请求传来的参数
    const { id } = ctx.request.query;
    // 判断数据库里面是否存在该用户
    const user = await ctx.service.user.Info(id);
    
    if(user){
        ctx.body = {
        code: 200,
        message:'登录成功',
        data: user,
        }
    }else{
      ctx.body = {
        code: 0,
        message:'查询失败',
        data: null,
        }
    } 
  }
}

module.exports = UserController;
